package com.efounder.util;

import android.content.Context;

import com.efounder.constant.EnvironmentVariable;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.utilcode.util.DeviceInfo;

import java.lang.reflect.Method;

/**
 * 手机推送功能相关类
 * @author YQS
 */
public class MobilePushUtils {
    public static String deviceCompany = DeviceInfo.GetManufacturer().replace(" ", "-");//meizu

    public static String deviceModel = DeviceInfo.GetModel().replace(" ", "-");
    private static final String TAG = "MobilePushUtils";

    /**
     * 判断设备是否支持推送
     *
     * @return
     */
    public static boolean isSupportPush() {

        if (!"1".equals(EnvironmentVariable.getProperty("isSupportPush", "0"))) {
            //如果应用没有注册，就算手机支持推送，也返回false
            return false;
        }


        if (isSupportHuaWeiPush()) {
            if (getEMUIVersion() >= 9) {
                return true;
            }
            return false;
        }
        if ("Xiaomi".equalsIgnoreCase(deviceCompany)) {
            return true;
        }
        //开启FCM推送
        if (isSupportGoogleService(AppContext.getInstance())) {
            // FIXME: 19-5-16 fcm支持之后再修改
            return false;
        }
        return false;
    }

    /**
     * 判断设备是否支持推送
     *
     * @return
     */
    public static boolean isSupportHuaWeiPush() {
        if ("HUAWEI".equalsIgnoreCase(deviceCompany)) {
            if ("Nexus-6P".equalsIgnoreCase(deviceModel)) {
                return false;
            }
            return true;
        }
        return false;
    }
    /**
     * 判断设备是否支持小米推送
     *
     * @return
     */
    public static boolean isSupportXiaoMiPush() {
        if ("Xiaomi".equalsIgnoreCase(deviceCompany)) {
            return true;
        }
        return false;
    }
    /**
     * 判断华为手机EMui的version(emuiApiLevel>=9 支持推送)
     *
     * @return
     */
    private static int getEMUIVersion() {
        int emuiApiLevel = 0;
        try {
            Class cls = Class.forName("android.os.SystemProperties");
            Method method = cls.getDeclaredMethod("get", new Class[]{String.class});
            emuiApiLevel = Integer.parseInt((String) method.invoke(cls, new Object[]{"ro.build.hw_emui_api_level"}));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return emuiApiLevel;
    }

    /**
     * 是否支持谷歌服务
     * @param context context
     */
    public static boolean isSupportGoogleService(Context context) {
        if (isSupportHuaWeiPush() || isSupportXiaoMiPush()) {
            return false;
        }
        try {
            //不知为何 catch 无效，所以先用反射
            Class.forName("com.google.android.gms.common.GoogleApiAvailability");
            GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
            int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context);
            return resultCode == ConnectionResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
